From 9d51791cfd19cdd6df8002b2ca77a0141963a4ed Mon Sep 17 00:00:00 2001 From: "Karl O. Pinc" Date: Tue, 6 Nov 2018 21:10:05 -0600 Subject: [PATCH] Reference css file by Pyramid asset specification --- setup.py | 1 + src/pgwui_common/__init__.py | 25 +++++++++- src/pgwui_common/templates/base.mak | 2 +- tests/test___init__.py | 73 ++++++++++++++++++++++++++++- 4 files changed, 98 insertions(+), 3 deletions(-) diff --git a/setup.py b/setup.py index 14651d2..9141c01 100644 --- a/setup.py +++ b/setup.py @@ -140,6 +140,7 @@ setup( # Run-time dependencies. install_requires=[ 'pgwui_core==' + version, + 'pyramid', 'pyramid_beaker', 'pyramid_mako', ], diff --git a/src/pgwui_common/__init__.py b/src/pgwui_common/__init__.py index 335bc8a..6f7add2 100644 --- a/src/pgwui_common/__init__.py +++ b/src/pgwui_common/__init__.py @@ -23,10 +23,33 @@ ''' +def base_view(wrapped): + '''Decorator for any view which includes base.mk. + ''' + def wrapper(request): + '''Add variables missing but needed by base.mk to the response. + ''' + response = wrapped(request) + pgwui = response.get('pgwui', {}) + pgwui.setdefault('url.css', + request.static_url('pgwui_common:static/pgwui.css')) + response['pgwui'] = pgwui + return response + return wrapper + + +def auth_base_view(wrapped): + '''Decorator for any view which includes auth_base.mk. + ''' + return base_view(wrapped) + + def includeme(config): '''Pyramid configuration for PGWUI_Common ''' config.include('pyramid_mako') config.include('pyramid_beaker') config.add_static_view( - 'static', 'pgwui_common:/static', cache_max_age=3600) + 'static', + 'pgwui_common:static/', + cache_max_age=3600) diff --git a/src/pgwui_common/templates/base.mak b/src/pgwui_common/templates/base.mak index daa3593..c51e2e6 100644 --- a/src/pgwui_common/templates/base.mak +++ b/src/pgwui_common/templates/base.mak @@ -45,7 +45,7 @@ <%block name="meta_keywords" /> <%block name="meta_description" /> - + diff --git a/tests/test___init__.py b/tests/test___init__.py index 917efd6..aa06b99 100644 --- a/tests/test___init__.py +++ b/tests/test___init__.py @@ -19,11 +19,75 @@ # Karl O. Pinc +import pyramid.config +import pyramid.testing +import pyramid.threadlocal +import pytest import pgwui_common.__init__ as pgwui_common_init -def test_configure_includecalled(): +# Fixtures +@pytest.fixture +def pyramid_config(): + yield pyramid.testing.setUp() + pyramid.testing.tearDown() + + +@pytest.fixture +def pyramid_request_config(): + request = pyramid.testing.DummyRequest() + yield pyramid.testing.setUp(request=request) + pyramid.testing.tearDown() + + +# Unit tests + +# base_view() +def test_base_view_add(pyramid_request_config): + '''The response adds all expected variables''' + def mock_view(request): + return {} + + pgwui_common_init.includeme(pyramid_request_config) + wrapper = pgwui_common_init.base_view(mock_view) + response = wrapper(pyramid.threadlocal.get_current_request()) + assert response['pgwui']['url.css'][0:4] == 'http' + + +def test_base_view_default(pyramid_request_config): + '''The response retains the mock view's variables''' + css_url = 'foo://bar/' + + def mock_view(request): + return {'pgwui': {'url.css': css_url}} + + pgwui_common_init.includeme(pyramid_request_config) + wrapper = pgwui_common_init.base_view(mock_view) + response = wrapper(pyramid.threadlocal.get_current_request()) + assert response['pgwui']['url.css'] == css_url + + +# auth_base_view() + +def test_auth_base_view(monkeypatch): + '''base_view() is called''' + base_view_called = False + + def mock_base_view(wrapped): + nonlocal base_view_called + base_view_called = True + + monkeypatch.setattr(pgwui_common_init, 'base_view', mock_base_view) + + pgwui_common_init.auth_base_view(None) + assert base_view_called is True + + +# includeme() + +def test_includeme_configurecalled(): + '''Pyramid Configure() methods are called''' class MockConfig(): def __init__(self): self.include_called = False @@ -39,3 +103,10 @@ def test_configure_includecalled(): pgwui_common_init.includeme(config) assert config.include_called assert config.add_static_view_called + + +# Integration tests + +def test_includeme(): + config = pyramid.config.Configurator() + pgwui_common_init.includeme(config) -- 2.34.1